package com.wbao.dianniu.ui;

import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.listener.IFeedbackListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.FeedbackManager;
import com.wbao.dianniu.utils.Notification;
import com.wbao.dianniu.R;

/**
 * 问题反馈
 * 
 * @author yejianjun
 *
 */
public class FeedbackActivity extends BaseActivity implements IFeedbackListener{
	
	private EditText et_content;
	private EditText et_contact;
	private Button submitBtn;
	private FeedbackManager manager;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentLayout(R.layout.activity_feedback);
		setTitleName("意见反馈");
		init();
		initManger();
		
	}
	private void init(){
		et_content = (EditText) findViewById(R.id.feedback_content);
		et_contact = (EditText) findViewById(R.id.feedback_contact);
		submitBtn = (Button) findViewById(R.id.feedback_submit);
		submitBtn.setOnClickListener(this);
	} 
	private void initManger(){
		manager = new FeedbackManager(this);
		manager.addFeedbackListener(this);
	}
	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.feedback_submit:
			if(TextUtils.isEmpty(et_content.getText().toString().trim())){
				Notification.toast(this, "请输入您的宝贵意见");
				return;
			}
			if(TextUtils.isEmpty(et_contact.getText().toString().trim())){
				Notification.toast(this, "请输入联系方式");
				return;
			}
			String version = getVersionName(this);
			manager.feedback(GlobalContext.accountId, et_content.getText().toString().trim(), et_contact.getText().toString().trim(), version,"a");
			break;

		default:
			break;
		}
	}
	/**
	 * 获取软件版本号
	 * 
	 * @param context
	 * @return
	 */
	private String getVersionName(Context context) {
		String versionName = null;
		try {
			// 获取软件版本号，对应AndroidManifest.xml下android:versionCode
			versionName = context.getPackageManager().getPackageInfo("com.wbao.dianniu", 0).versionName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return versionName;
	}
	@Override
	public void onFeedbackFailure(int errorCode, String errorMsg) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onFeedbackSuccess() {
		// TODO Auto-generated method stub
		Notification.toast(this, "反馈成功");
		finish();
	}
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
		}
		public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
		}
}
